package GUI;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import server.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;

@SuppressWarnings("serial")
/**
 * Runs the server GUI and starts the Server and the SQL connection
 * @author Benny
 * @param con connection with specific SQL DB
 * @param server connection with specific server
 */
public class ServerGUI extends JFrame {
	public ReturnsThread daily;
	public JLabel lblNewLabel;
	public JLabel label;
	mysqlConnection con;
	ObservableServer server;
	private JTextField textField_2;
	public JLabel lblIp;
	public ServerGUI() {
		setTitle("Server");
		setResizable(false);
		setPreferredSize(new Dimension(300, 300));
		setMaximumSize(new Dimension(300, 300));
		setMinimumSize(new Dimension(300, 300));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		
		JLabel lblDbUsernameAnd = new JLabel("Enter port for connection:");
		lblDbUsernameAnd.setBounds(10, 12, 188, 14);
		getContentPane().add(lblDbUsernameAnd);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					
				
				con=new mysqlConnection();
				lblNewLabel.setText("DB Connection Succesful");
				lblNewLabel.setForeground(Color.BLACK);
				}catch(Exception ex){
					lblNewLabel.setText("DB Connection Failed");
					lblNewLabel.setForeground(Color.RED);
					
					
				}
				
				 int port = 0; //Port to listen on09

				    try
				    {
				      port = Integer.parseInt(textField_2.getText());
				    }
				    catch(Throwable t)
				    {
				      port =5555; //Set port to 5555
				    }
					
				  server=new ObservableServer(port,con);
				  
	                InetAddress ip = null;
	                try {

	                        ip = InetAddress.getLocalHost();

	                        System.out.println("Current IP address : " + ip.getHostAddress());

	                } catch (UnknownHostException e) {

	                        e.printStackTrace();

	                }
	                lblIp.setText("IP: " + ip.getHostAddress()+":" + port);
	                lblIp.setForeground(Color.BLUE);
				    try 
				    {
				      server.listen(); //Start listening for connections
				      label.setText("Server: Listening to clients");
				     label.setForeground(Color.BLACK);
				    } 
				    catch (Exception ex) 
				    {
				      label.setText("Server: Could not listen for clients!");
				     label.setForeground(Color.RED);
				    }
				    
					daily = new ReturnsThread("daily", con);
				    daily.start();
				
			}
		});
		btnOk.setBounds(102, 70, 89, 23);
		getContentPane().add(btnOk);
		
		 lblNewLabel = new JLabel(" ");
		lblNewLabel.setBounds(10, 131, 267, 14);
		getContentPane().add(lblNewLabel);
		
		textField_2 = new JTextField();
		textField_2.setText("5555");
		textField_2.setColumns(10);
		textField_2.setBounds(109, 38, 107, 20);
		getContentPane().add(textField_2);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(30, 38, 56, 14);
		getContentPane().add(lblPort);
		
		JLabel lblServerConnection = new JLabel("Server Status:");
		lblServerConnection.setBounds(10, 105, 152, 14);
		getContentPane().add(lblServerConnection);
		        
        lblIp = new JLabel("IP:");
        lblIp.setBounds(10, 174, 181, 25);
        getContentPane().add(lblIp);
		
        label = new JLabel(" ");
		label.setBounds(10, 154, 267, 14);
		getContentPane().add(label);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(-5, 0);
		picLabel.setSize(299, 272);
		getContentPane().add(picLabel);
		
		this.setVisible(true);
	}
	/**
	 * runs the Server GUI
	 * @param args
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ServerGUI X=new ServerGUI();

	}
	
	
}
